package com.example.demo.practice;

import java.util.*;
import java.util.stream.Collectors;

/**
 * @author liweizhi
 * @date 2020/4/26
 */
public class StringAppend {
    public static void main(String[] args) {
//        appendTest();
//        listMap();

        List<String> list = new ArrayList<>();
        list.add("1");
        list.add("2");
        for (String item : list) {
            if ("2".equals(item)) {
                list.remove(item);
            }
        }
        System.out.println(list);
    }

    private static void listMap() {
        List<Pair<String, Double>> pairArrayList = new ArrayList<>(3);

        pairArrayList.add(new Pair<>("version", 6.19));
        pairArrayList.add(new Pair<>("version", null));
        pairArrayList.add(new Pair<>("version", 13.14));
        Map<String, Double> map = pairArrayList.stream().
                filter(pair -> pair.getValue() != null)
                .collect(
                        // 生成的 map 集合中只有一个键值对：{version=13.14}
                        Collectors.toMap(Pair::getKey, Pair::getValue, (v1, v2) -> v2)
                /*Collectors.toMap(Pair::getKey, Pair::getValue, new BinaryOperator<Double>() {
                    @Override
                    public Double apply(Double v1, Double v2) {
                        return v2 == null ? v1 : v2;
                    }
                })*/
                );
        System.out.println(map);
    }

    private static class Pair<K, V> {
        private K key;
        private V value;

        public Pair(K key, V value) {
            this.key = key;
            this.value = value;
        }

        public K getKey() {
            return key;
        }

        public void setKey(K key) {
            this.key = key;
        }

        public V getValue() {
            return value;
        }

        public void setValue(V value) {
            this.value = value;
        }
    }

    private static void appendTest() {
        String str = "start";
        for (int i = 0; i < 100; i++) {
            str = str + "hello";
        }
        System.out.println(str);
    }
}
